//买礼物
//https://www.luogu.com.cn/problem/P1194
#include <iostream>
#include <vector>
using namespace std;

const int N = 5e3 + 10;
typedef struct
{
	int a; int b; int len;
}node;

vector<node> arr;

int uf[N];

int find(int n)
{
	return n == uf[n] ? n : uf[n] = find(uf[n]);
}

int main()
{
	int A, B;
	cin >> A >> B;
	//初始化并查集
	for (int i = 0; i < N; i++) uf[i] = i;

	for (int i = 1; i <= B; i++)
	{
		for (int j = 1; j <= B; j++)
		{
			int tmp; cin >> tmp;
			if (tmp == 0) continue;
			//合适的边，要存
			arr.push_back({ i,j,tmp });
		}
	}
	
	//排序
	sort(arr.begin(), arr.end(), [](const node& a, const node& b) {return a.len < b.len; });

	//最终选的边数
	int count = 0; 
	int n = arr.size();
	//结果
	int ret = 0;
	for (int i = 0; i < n; i++)
	{
		int a = arr[i].a; int b = arr[i].b; int len = arr[i].len;
		if (len > A) continue;
		int f1 = find(a); int f2 = find(b);
		if (f1 != f2)
		{
			ret += len;
			uf[f1] = f2;
			count++;
		}
	}

	//计算最终结果
	ret += A * (B - count);
	cout << ret << endl;
	return 0;
}
